
package com.shop.cloud.mall.admin.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.cloud.common.core.constant.CommonConstants;
import com.shop.cloud.common.core.util.R;
import com.shop.cloud.common.log.annotation.SysLog;
import com.shop.cloud.mall.admin.service.FreightTemplatService;
import com.shop.cloud.mall.admin.service.GoodsSpuService;
import com.shop.cloud.mall.common.constant.MallConstants;
import com.shop.cloud.mall.common.entity.FreightTemplat;
import com.shop.cloud.mall.common.entity.GoodsSpu;
import com.shop.cloud.mall.common.entity.SupplierGoodsSpu;
import com.shop.cloud.mall.admin.service.SupplierGoodsSpuService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;

import java.util.List;

/**
 * 供应商spu表
 *
 * @author 
 * @date 2024-11-06 10:08:57
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/suppliergoodsspu")
@Tag(description = "suppliergoodsspu", name = "供应商spu表管理")
public class SupplierGoodsSpuController {

    private final SupplierGoodsSpuService supplierGoodsSpuService;
    private final FreightTemplatService freightTemplatService;
    private final GoodsSpuService goodsSpuService;

    /**
     * list查询
     * @param supplierGoodsSpu
     * @return
     */
    @Operation(summary = "供应商list")
    @GetMapping("/list")
    public R getList(SupplierGoodsSpu supplierGoodsSpu) {
        return R.ok(supplierGoodsSpuService.list(Wrappers.query(supplierGoodsSpu).lambda()
                .select(SupplierGoodsSpu::getId,
                        SupplierGoodsSpu::getName,
                        SupplierGoodsSpu::getSupplierId)));
    }

    /**
     * 供应商spu表分页列表
     * @param page 分页对象
     * @param supplierGoodsSpu 供应商spu表
     * @return
     */
    @Operation(summary = "供应商spu表分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ato.hasAuthority('mall:suppliergoodsspu:index')")
    public R getPage(Page page, SupplierGoodsSpu supplierGoodsSpu) {
        return R.ok(supplierGoodsSpuService.page1(page, supplierGoodsSpu));
    }

    /**
     * 供应商spu表查询
     * @param id
     * @return R
     */
    @Operation(summary = "供应商spu表查询")
    @GetMapping("/{id}")
    @PreAuthorize("@ato.hasAuthority('mall:suppliergoodsspu:get')")
    public R getById(@PathVariable("id") String id) {
        return R.ok(supplierGoodsSpuService.getById1(id));
    }

    /**
     * 供应商spu表新增
     * @param supplierGoodsSpu 供应商spu表
     * @return R
     */
    @Operation(summary = "供应商spu表新增")
    @SysLog("新增供应商spu表")
    @PostMapping
    @PreAuthorize("@ato.hasAuthority('mall:suppliergoodsspu:add')")
    public R save(@RequestBody SupplierGoodsSpu supplierGoodsSpu) {
        if(MallConstants.FREIGHT_TYPE_2.equals(supplierGoodsSpu.getFreightType())){
            FreightTemplat freightTemplat = freightTemplatService.getById(supplierGoodsSpu.getFreightTemplatId());
            if(!supplierGoodsSpu.getSupplierId().equals(freightTemplat.getShopId())){
                return R.failed("请选择商品所属供应商下的运费模板");
            }
        }
        try {
            return R.ok(supplierGoodsSpuService.save1(supplierGoodsSpu));
        }catch (DuplicateKeyException e){
            if(e.getMessage().contains("uk_code")){
                return R.failed("商品编码已存在");
            }else{
                return R.failed(e.getMessage());
            }
        }
    }

    /**
     * 批量新增spu商品
     * @param listSupplierGoodsSpu spu商品集合
     * @return R
     */
    @Operation(summary = "批量新增spu商品")
    @SysLog("批量新增spu商品")
    @PostMapping("/batch")
    @PreAuthorize("@ato.hasAuthority('mall:goodsspu:add')")
    public R saveBatch(@RequestBody List<SupplierGoodsSpu> listSupplierGoodsSpu){
        try {
            return supplierGoodsSpuService.saveBatch1(listSupplierGoodsSpu);
        }catch (DuplicateKeyException e){
            if(e.getMessage().contains("uk_code")){
                return R.failed("商品编码已存在"+e.getMessage());
            }else{
                return R.failed(e.getMessage());
            }
        }
    }

    /**
     * 供应商spu表修改
     * @param supplierGoodsSpu 供应商spu表
     * @return R
     */
    @Operation(summary = "供应商spu表修改")
    @SysLog("修改供应商spu表")
    @PutMapping
    @PreAuthorize("@ato.hasAuthority('mall:suppliergoodsspu:edit')")
    public R updateById(@RequestBody SupplierGoodsSpu supplierGoodsSpu) {
        SupplierGoodsSpu supplierGoodsSpu2 = supplierGoodsSpuService.getById(supplierGoodsSpu.getId());
        if(CommonConstants.YES.equals(supplierGoodsSpu2.getShelf())){
            return R.failed("已上架商品不能修改");
        }
        supplierGoodsSpu.setShelf(null);
        if(MallConstants.FREIGHT_TYPE_2.equals(supplierGoodsSpu.getFreightType())){
            FreightTemplat freightTemplat = freightTemplatService.getById(supplierGoodsSpu.getFreightTemplatId());
            if(!supplierGoodsSpu.getSupplierId().equals(freightTemplat.getShopId())){
                return R.failed("请选择商品所属供应商下的运费模板");
            }
        }
        try {
            return R.ok(supplierGoodsSpuService.updateById1(supplierGoodsSpu));
        }catch (DuplicateKeyException e){
            if(e.getMessage().contains("uk_code")){
                return R.failed("商品编码已存在");
            }else{
                return R.failed(e.getMessage());
            }
        }
    }

    /**
     * 商品上下架操作
     * @param shelf
     * @param ids
     * @return R
     */
    @Operation(summary = "商品上下架操作")
    @SysLog("商品上下架操作")
    @PutMapping("/shelf")
    @PreAuthorize("@ato.hasAuthority('mall:goodsspu:edit')")
    public R shelf(@RequestParam(value = "shelf") String shelf, @RequestParam(value = "ids") String ids){
        SupplierGoodsSpu supplierGoodsSpu = new SupplierGoodsSpu();
        supplierGoodsSpu.setShelf(shelf);
        supplierGoodsSpuService.shelf(shelf, ids);;
        return R.ok();
    }

    /**
     * 供应商spu表删除
     * @param id
     * @return R
     */
    @Operation(summary = "供应商spu表删除")
    @SysLog("删除供应商spu表")
    @DeleteMapping("/{id}")
    @PreAuthorize("@ato.hasAuthority('mall:suppliergoodsspu:del')")
    public R removeById(@PathVariable String id) {
        SupplierGoodsSpu supplierGoodsSpu = supplierGoodsSpuService.getById(id);
        if(CommonConstants.YES.equals(supplierGoodsSpu.getShelf())){
            return R.failed("已上架商品不能删除");
        }
        //有关联店铺商品不能删除
        long c = goodsSpuService.count(Wrappers.<GoodsSpu>lambdaQuery()
                .eq(GoodsSpu::getSupplierSpuId,id));
        if(c > 0){
            return R.failed("有关联店铺商品不能删除");
        }
        return R.ok(supplierGoodsSpuService.removeById(id));
    }

}
